package com.cssw.fyzb.core.dao.live;

import com.cssw.fyzb.core.model.dodb.*;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class LiveDaoImpl implements LiveDao {
    public CmfLive findOne(Long liveId) {
        return CmfLive.dao.findById(liveId);
    }

    @Override
    public CmfLive getLivingByUserId(Long userId) {
        String sql = "select * from cmf_live where user_id = ? and status = ? order by live_id desc";
        return CmfLive.dao.findFirst(sql, userId, CmfLive.STATUS_LIVING);
    }

    @Override
    public Long createRoom(
            Long userId, Long anchorId, String title, String thumb, String push, String pull, String accelerate,
            String lat, String lng, String province, String city
    ) {
        CmfLive live = new CmfLive();
        live.setUserId(userId);
        live.setAnchorId(anchorId);
        live.setTitle(title);
        live.setThumb(thumb);
        live.setPull(pull);
        live.setPush(push);
        live.setAccelerate(accelerate);
        live.setLat(lat);
        live.setLng(lng);
        live.setProvince(province);
        live.setCity(city);
        live.setStartTime(new Date());
        live.setStatus("1");
        live.save();
        return live.getLiveId();
    }

    @Override
    public void incLikes(Long liveId, Integer likes) {
        CmfLive live = findOne(liveId);
        if (live != null) {
            live.setLikeNum(live.getLikeNum() + likes);
            live.update();
        }
    }

    @Override
    public void incViews(Long liveId, Integer views) {
        CmfLive live = findOne(liveId);
        if (live != null) {
            live.setLookNum(live.getLookNum() + views);
            live.update();
        }
    }

    @Override
    public void pk(Long liveId, String pk) {
        CmfLive live = findOne(liveId);
        if (live != null) {
            live.setLianmai(pk);
            live.update();
        }
    }

    @Override
    public Page<CmfLive> listLiveFriend(Long userId, Integer pageNumber, Integer pageSize) {
        Map param = new HashMap();
        param.put("userId", userId);
        param.put("relType", UserRel.REL_TYPE_FRIEND);
        param.put("status", CmfLive.STATUS_LIVING);

        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL(" select t1.* from cmf_live t1, t_user_rel t2 ");
        sqlParse.addSQL(" where t1.user_id = t2.rel_user_id ");
        sqlParse.addSQL(" and t2.user_id = :userId ");
        sqlParse.addSQL(" and t2.rel_type = :relType ");
        sqlParse.addSQL(" and t1.status = :status ");
        sqlParse.addSQL(" and (t1.lianmai = '0' or t1.lianmai is null) ");

        return CmfLive.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public void dropRoom(Long liveId) {
        CmfLive live = findOne(liveId);
        if (live != null) {
            Date now = new Date();
            live.setStatus("2");
            live.setEndTime(now);
            live.setTimeLong((int) ((now.getTime() - live.getStartTime().getTime()) / 1000));
            live.update();
        }
    }

    @Override
    public CmfLiveKickOut queryLiveKickOutById(Long id) {
        return CmfLiveKickOut.dao.findById(id);
    }

    @Override
    public CmfLiveKickOut queryLiveKickOut(Long liveId, Long operUserId, Long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("liveId", liveId);
        params.put("operUserId", operUserId);
        params.put("userId", userId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select * from cmf_live_kick_out where live_id=:liveId and oper_user_id=:operUserId and user_id=:userId");
        CmfLiveKickOut cmfLiveKickOut = CmfLiveKickOut.dao.findFirst(sqlParse);
        return cmfLiveKickOut;
    }

    @Override
    public Long addLiveKickOut(Long liveId, Long operUserId, Long userId) {
        CmfLiveKickOut cmfLiveKickOut = new CmfLiveKickOut();
        cmfLiveKickOut.setLiveId(liveId);
        cmfLiveKickOut.setOperUserId(operUserId);
        cmfLiveKickOut.setUserId(userId);
        cmfLiveKickOut.save();
        return cmfLiveKickOut.getKoId();
    }

    @Override
    public void removeLiveKickOutById(Long id) {
        CmfLiveKickOut cmfLiveKickOut = CmfLiveKickOut.dao.findById(id);
        if (cmfLiveKickOut != null) {
            cmfLiveKickOut.delete();
        }
    }

    @Override
    public Page<CmfLiveKickOut> listLiveKickOut(Long liveId, Integer pageNumber, Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("liveId", liveId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select * from cmf_live_kick_out where live_id=:liveId");
        return CmfLiveKickOut.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public List<CmfLiveKickOut> listLiveKickOut(Long liveId) {
        Map<String, Object> params = new HashMap<>();
        params.put("liveId", liveId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select * from cmf_live_kick_out where live_id=:liveId");
        return CmfLiveKickOut.dao.find(sqlParse);
    }

    @Override
    public CmfLiveMute queryLiveMuteById(Long id) {
        return CmfLiveMute.dao.findById(id);
    }

    @Override
    public CmfLiveMute queryLiveMute(Long liveId, Long operUserId, Long userId) {
        Map<String, Object> params = new HashMap<>();
        params.put("liveId", liveId);
        params.put("operUserId", operUserId);
        params.put("userId", userId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select * from cmf_live_mute where live_id=:liveId and oper_user_id=:operUserId and user_id=:userId");
        CmfLiveMute cmfLiveMute = CmfLiveMute.dao.findFirst(sqlParse);
        return cmfLiveMute;
    }

    @Override
    public Long addLiveMute(Long liveId, Long operUserId, Long userId) {
        CmfLiveMute cmfLiveMute = new CmfLiveMute();
        cmfLiveMute.setLiveId(liveId);
        cmfLiveMute.setOperUserId(operUserId);
        cmfLiveMute.setUserId(userId);
        cmfLiveMute.save();
        return cmfLiveMute.getMuteId();
    }

    @Override
    public void removeLiveMuteById(Long id) {
        CmfLiveMute cmfLiveMute = CmfLiveMute.dao.findById(id);
        if (cmfLiveMute != null) {
            cmfLiveMute.delete();
        }
    }

    @Override
    public Page<CmfLiveMute> listLiveMute(Long liveId, Integer pageNumber, Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("liveId", liveId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select * from cmf_live_mute where live_id=:liveId");
        return CmfLiveMute.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public List<CmfLiveMute> listLiveMute(Long liveId) {
        Map<String, Object> params = new HashMap<>();
        params.put("liveId", liveId);
        SqlParse sqlParse = new SqlParse(params);
        sqlParse.addSQL("select * from cmf_live_mute where live_id=:liveId");
        return CmfLiveMute.dao.find(sqlParse);
    }

    @Override
    public Page<CmfLive> listLive(Map param, Integer pageNumber, Integer pageSize) {
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("select * from cmf_live where 1=1");
        sqlParse.addSQL(" and city=:city");
        sqlParse.addSQL(" and user_id=:userId");
        sqlParse.addSQL(" and status=:status");
        return CmfLive.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public Page<CmfLive> randomPkLive(Map param, Integer pageNumber, Integer pageSize) {
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL(" select * from cmf_live t1  ");
        sqlParse.addSQL(" where t1.live_id >= ( ");
        sqlParse.addSQL(" SELECT FLOOR( MAX(t2.live_id) * RAND()) FROM cmf_live t2  ");
        sqlParse.addSQL(" where t2.status='1' ");
        sqlParse.addSQL(" and (t2.lianmai='0' or t2.lianmai is null) ");
        sqlParse.addSQL(" and t2.city=:city ");
        sqlParse.addSQL(" )");
        sqlParse.addSQL(" and t1.status='1' ");
        sqlParse.addSQL(" and (t1.lianmai='0' or t1.lianmai is null) ");
        sqlParse.addSQL(" and t1.city=:city ");
        sqlParse.addSQL(" and t1.user_id !=:userId ");
        return CmfLive.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public Page<CmfLive> searchLive(Map param, Integer pageNumber, Integer pageSize) {
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("select * from cmf_live where 1=1");
        sqlParse.addSQL(" and user_id=:key");
        sqlParse.addSQL(" and status=:status");
        return CmfLive.dao.paginate(pageNumber, pageSize, sqlParse);
    }

    @Override
    public void updateLianmai(Long liveId, String lianmai) {
        CmfLive live = new CmfLive();
        live.setLiveId(liveId);
        live.setLianmai(lianmai);
        live.update();
    }

    public CmfUserLive getCmfUserLive(Long uid) {
        return CmfUserLive.dao.findById(uid);
    }
}
